package com.js.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.blog.bean.Article;
import com.js.blog.bean.User;
import com.js.blog.bean.UserPermission;
import com.js.blog.consts.ConstObject;
import com.js.blog.bean.vo.LoginVo;
import com.js.blog.bean.vo.UserVo;
import com.js.blog.exception.BusinessException;
import com.js.blog.exception.OtherErrorCode;
import com.js.blog.result.ResponseEntity;
import com.js.blog.service.ArticleService;
import com.js.blog.service.UserPermissionService;
import com.js.blog.service.UserService;
import com.js.blog.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author yanglin
 * @create 2024-02-29 15:27 星期四
 * description:
 */
@RestController
public class AdminController {

    @Autowired
    private UserService userService;
    
    @Autowired
    ArticleService articleService;
    
    @Autowired
    UserPermissionService userPermissionService;
    
    /**
     * 登录接口
     * @return
     */
    @PostMapping("/admin/login")
    public ResponseEntity login(@RequestBody LoginVo loginVo){
        //1、非空校验
        if (loginVo == null){
            return new ResponseEntity(OtherErrorCode.USER_NOT_NULL);
        }
        
        if (StringUtils.isEmpty(loginVo.getUsername()) || StringUtils.isEmpty(loginVo.getPassword())){
            return new ResponseEntity(OtherErrorCode.USERNAME_OR_PASS_NULL); 
        }
        
        //2、校验数据库
        UserVo userVo = userService.login(loginVo);
        
        if (userVo == null){
            //用户名或密码错误
            return new ResponseEntity(OtherErrorCode.USER_USERNAME_OR_PASS_ERROR);
            
        }
        return new ResponseEntity(200,"成功",userVo);
    }

    /**
     * 注册接口
     */
    @PostMapping("/admin/register")
    public ResponseEntity register(@RequestBody User user){
        //1、非空校验
        if (user == null){
            return new ResponseEntity(OtherErrorCode.USER_NOT_NULL);
        }
        //2、参数校验
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())){
            return new ResponseEntity(OtherErrorCode.USERNAME_OR_PASS_NULL);
        }
        
        //3、校验用户名是否存在
        boolean ifExist = userService.ifExistUsername(user.getUsername());
        if (ifExist){
           return new ResponseEntity(OtherErrorCode.USER_NAME_EXIST);
        }
        
        //对前端传递的密码进行MD5加密
        user.setPassword(MD5.encrypt(user.getPassword()));

        Date now = new Date();
        user.setCreateTime(now);
        //入库
        userService.save(user);
        
        return ResponseEntity.ok();
    }
    
    
    @PostMapping("/admin/publish/{userId}")
    public ResponseEntity publishArticle(@PathVariable("userId") Long userId, @RequestBody Article article){
        //判断当前用户是否存在
        User user = userService.getById(userId);
        if (user == null){
            throw new BusinessException(OtherErrorCode.USER_NOT_LOGIN);
        }
        
        //参数校验
        if (article == null){
            throw new BusinessException(OtherErrorCode.PUBLISH_ARTICLE_NOT_EXIST);
        }

        //判断用户是否具有发布权限
        List<UserPermission> userPermissions = userPermissionService.getByUserId(userId);
        for (UserPermission userPermission : userPermissions) {
            if (!ConstObject.PUBLISH_ARTICLE.equals(userPermission.getPermissionName())){   //代表不具有发布权限
                throw new BusinessException(OtherErrorCode.NOT_PUBLISH_PERMISSION);
            }
        }
        article.setUserId(userId);
        article.setCreateTime(new Date());
        articleService.save(article);
        return ResponseEntity.ok();
    }
    
    
    
    @GetMapping("/admin/article/list/{id}/{page}/{limit}")
    public ResponseEntity articleList(@PathVariable("id") Long id,
                                      @PathVariable("page") Long page,
                                      @PathVariable("limit") Long limit){

        //校验参数
        if (id == null || id == 0){
            return new ResponseEntity(OtherErrorCode.USER_ID_NULL);
        }
        Page<Article> articlePage = new Page<>(page, limit);
        IPage<Article> pageModel = articleService.selectPage(articlePage,id);
        return new ResponseEntity(200,"成功",pageModel);
    }
    
    
    
}
